package easier.nested.scroll;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import androidx.annotation.Nullable;
import androidx.core.view.NestedScrollingChild2;
import androidx.core.view.NestedScrollingChildHelper;
import androidx.core.view.ViewCompat;

/**
 * @author Liujin 2018-09-20:11:49
 */
public class TestNestedChildView extends View implements NestedScrollingChild2 {

    private static final String TAG = TestNestedChildView.class.getSimpleName();

    private NestedScrollingChildHelper mChildHelper;

    private float mDownX;
    private float mDownY;

    private int[] mConsumed = new int[2];
    private int[] mOffsetInWindow = new int[4];

    public TestNestedChildView( Context context ) {

        this( context, null, 0 );
    }

    public TestNestedChildView(
        Context context, @Nullable AttributeSet attrs ) {

        this( context, attrs, 0 );
    }

    public TestNestedChildView(
        Context context, @Nullable AttributeSet attrs, int defStyleAttr ) {

        super( context, attrs, defStyleAttr );
        init();
    }

    private void init() {

        mChildHelper = new NestedScrollingChildHelper( this );
        setNestedScrollingEnabled( true );
    }

    @Override
    public boolean onTouchEvent( MotionEvent event ) {

        float x = event.getX();
        float y = event.getY();

        int action = event.getAction();
        if ( action == MotionEvent.ACTION_DOWN ) {

            mDownX = x;
            mDownY = y;

            startNestedScroll(
                ViewCompat.SCROLL_AXIS_HORIZONTAL | ViewCompat.SCROLL_AXIS_VERTICAL,
                ViewCompat.TYPE_TOUCH
            );
        }

        if ( action == MotionEvent.ACTION_MOVE ) {
            int dx = (int) (x - mDownX);
            int dy = (int) (y - mDownY);
            mConsumed[0] = 0;
            mConsumed[1] = 0;
            dispatchNestedPreScroll( dx, dy, mConsumed, mOffsetInWindow,
                ViewCompat.TYPE_TOUCH
            );
            dx -= mConsumed[0];
            dy -= mConsumed[1];

            offsetLeftAndRight( dx );
            offsetTopAndBottom( dy );
        }

        if ( action == MotionEvent.ACTION_UP ) {
            stopNestedScroll( ViewCompat.TYPE_TOUCH );
        }

        return true;
    }

    @Override
    public void setNestedScrollingEnabled( boolean enabled ) {

        super.setNestedScrollingEnabled( enabled );
        mChildHelper.setNestedScrollingEnabled( enabled );
    }

    @Override
    public boolean isNestedScrollingEnabled() {

        return mChildHelper.isNestedScrollingEnabled();
    }

    @Override
    public boolean startNestedScroll( int axes, int type ) {

        return mChildHelper.startNestedScroll( axes, type );
    }

    @Override
    public void stopNestedScroll( int type ) {

        mChildHelper.stopNestedScroll( type );
    }

    @Override
    public boolean hasNestedScrollingParent( int type ) {

        return mChildHelper.hasNestedScrollingParent( type );
    }

    @Override
    public boolean dispatchNestedScroll(
        int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed,
        @Nullable int[] offsetInWindow, int type ) {

        return mChildHelper
            .dispatchNestedScroll( dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed,
                offsetInWindow, type
            );
    }

    @Override
    public boolean dispatchNestedPreScroll(
        int dx, int dy, @Nullable int[] consumed, @Nullable int[] offsetInWindow, int type ) {

        return mChildHelper.dispatchNestedPreScroll( dx, dy, consumed, offsetInWindow, type );
    }
}
